/**
 * @file BinaryOperator.H
 * @brief 
 * @author htli (22235058@zju.edu.cn)
 * @version 1.0
 * @date 2022-11-11
 * 
 * @copyright Copyright (c) 2022  linhuo2020
 * 
 */
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

const char Operator[7]{'+', '-', '*', '/', '^', '(', ')'};

inline bool isOpt(const char &opt)
{
    for (auto k : Operator)
        if (k == opt)
            return true;
    return false;
};

inline bool isNumber(const char &opt)
{
    return ('0' <= opt) && (opt <= '9');
};

inline bool isNumber(const string &opt)
{
    int NumOfDot = 0;
    for (auto k : opt)
        if (!isNumber(k))
        {
            if (k == '.')
                NumOfDot++;
            else
                return false;
        }
    return (NumOfDot <= 1);
}

inline int OpPriorLevel(const char &opt)
{
    switch (opt)
    {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    case '^':
        return 3;
    default:
        return -1;
    }
};

inline bool BinaryOpCompute(const double &_LeftNum, const double &_RightNum, const string &op, double &_result)
{
    switch (op[0])
    {
    case '+':
        _result = _LeftNum + _RightNum;
        break;
    case '-':
        _result = _LeftNum - _RightNum;
        break;
    case '*':
        _result = _LeftNum * _RightNum;
        break;
    case '/':
        if (_RightNum == 0)
        {
            cerr << "Divided by Zero\n";
            return false;
        }
        _result = _LeftNum / _RightNum;
        break;
    case '^':
        _result = pow(_LeftNum, _RightNum);
        break;
    default:
        if(op[0] =='('||op[0] == ')')
        cerr << "Brackets are unmatched\n";
        else
        cerr << "No such Operator " << op[0] << "\n";
        return false;
    }
    return true;
}
